3 * Contain classes for dealing with individual log entries
5 * This is how I see the log system history:
6 * - appending to plain wiki pages
7 * - formatting log entries based on database fields
8 * - user is now part of the action message
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 * @author Niklas Laxström
27 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
31 use Wikimedia\Rdbms\IDatabase
;
34 * Interface for log entries. Every log entry has these methods.
45 public function getType();
52 public function getSubtype();
55 * The full logtype in format maintype/subtype.
59 public function getFullType();
62 * Get the extra parameters stored for this message.
66 public function getParameters();
69 * Get the user for performed this action.
73 public function getPerformer();
76 * Get the target page of this action.
80 public function getTarget();
83 * Get the timestamp when the action was executed.
87 public function getTimestamp();
90 * Get the user provided comment.
94 public function getComment();
97 * Get the access restriction.
101 public function getDeleted();
104 * @param int $field One of LogPage::DELETED_* bitfield constants
107 public function isDeleted( $field );
111 * Extends the LogEntryInterface with some basic functionality
115 abstract class LogEntryBase
implements LogEntry
{
117 public function getFullType() {
118 return $this->getType() . '/' . $this->getSubtype();
121 public function isDeleted( $field ) {
122 return ( $this->getDeleted() & $field ) === $field;
126 * Whether the parameters for this log are stored in new or
131 public function isLegacy() {
136 * Create a blob from a parameter array
139 * @param array $params
142 public static function makeParamBlob( $params ) {
143 return serialize( (array)$params );
147 * Extract a parameter array from a blob
150 * @param string $blob
153 public static function extractParams( $blob ) {
154 return unserialize( $blob );
159 * This class wraps around database result row.
163 class DatabaseLogEntry
extends LogEntryBase
{
166 * Returns array of information that is needed for querying
167 * log entries. Array contains the following keys:
168 * tables, fields, conds, options and join_conds
172 public static function getSelectQueryData() {
173 $tables = [ 'logging', 'user' ];
175 'log_id', 'log_type', 'log_action', 'log_timestamp',
176 'log_user', 'log_user_text',
177 'log_namespace', 'log_title', // unused log_page
178 'log_comment', 'log_params', 'log_deleted',
179 'user_id', 'user_name', 'user_editcount',
183 // IPs don't have an entry in user table
184 'user' => [ 'LEFT JOIN', 'log_user=user_id' ],
192 'join_conds' => $joins,
197 * Constructs new LogEntry from database result row.
198 * Supports rows from both logging and recentchanges table.
200 * @param stdClass|array $row
201 * @return DatabaseLogEntry
203 public static function newFromRow( $row ) {
205 if ( isset( $row->rc_logid
) ) {
206 return new RCDatabaseLogEntry( $row );
208 return new self( $row );
212 /** @var stdClass Database result row. */
216 protected $performer;
218 /** @var array Parameters for log entry */
221 /** @var int A rev id associated to the log entry */
222 protected $revId = null;
224 /** @var bool Whether the parameters for this log entry are stored in new or old format. */
227 protected function __construct( $row ) {
232 * Returns the unique database id.
236 public function getId() {
237 return (int)$this->row
->log_id
;
241 * Returns whatever is stored in the database field.
245 protected function getRawParameters() {
246 return $this->row
->log_params
;
249 public function isLegacy() {
250 // This extracts the property
251 $this->getParameters();
252 return $this->legacy
;
255 public function getType() {
256 return $this->row
->log_type
;
259 public function getSubtype() {
260 return $this->row
->log_action
;
263 public function getParameters() {
264 if ( !isset( $this->params
) ) {
265 $blob = $this->getRawParameters();
266 MediaWiki\
suppressWarnings();
267 $params = LogEntryBase
::extractParams( $blob );
268 MediaWiki\restoreWarnings
();
269 if ( $params !== false ) {
270 $this->params
= $params;
271 $this->legacy
= false;
273 $this->params
= LogPage
::extractParams( $blob );
274 $this->legacy
= true;
277 if ( isset( $this->params
['associated_rev_id'] ) ) {
278 $this->revId
= $this->params
['associated_rev_id'];
279 unset( $this->params
['associated_rev_id'] );
283 return $this->params
;
286 public function getAssociatedRevId() {
287 // This extracts the property
288 $this->getParameters();
292 public function getPerformer() {
293 if ( !$this->performer
) {
294 $userId = (int)$this->row
->log_user
;
295 if ( $userId !== 0 ) {
297 if ( isset( $this->row
->user_name
) ) {
298 $this->performer
= User
::newFromRow( $this->row
);
300 $this->performer
= User
::newFromId( $userId );
304 $userText = $this->row
->log_user_text
;
305 $this->performer
= User
::newFromName( $userText, false );
309 return $this->performer
;
312 public function getTarget() {
313 $namespace = $this->row
->log_namespace
;
314 $page = $this->row
->log_title
;
315 $title = Title
::makeTitle( $namespace, $page );
320 public function getTimestamp() {
321 return wfTimestamp( TS_MW
, $this->row
->log_timestamp
);
324 public function getComment() {
325 return $this->row
->log_comment
;
328 public function getDeleted() {
329 return $this->row
->log_deleted
;
333 class RCDatabaseLogEntry
extends DatabaseLogEntry
{
335 public function getId() {
336 return $this->row
->rc_logid
;
339 protected function getRawParameters() {
340 return $this->row
->rc_params
;
343 public function getAssociatedRevId() {
344 return $this->row
->rc_this_oldid
;
347 public function getType() {
348 return $this->row
->rc_log_type
;
351 public function getSubtype() {
352 return $this->row
->rc_log_action
;
355 public function getPerformer() {
356 if ( !$this->performer
) {
357 $userId = (int)$this->row
->rc_user
;
358 if ( $userId !== 0 ) {
359 $this->performer
= User
::newFromId( $userId );
361 $userText = $this->row
->rc_user_text
;
362 // Might be an IP, don't validate the username
363 $this->performer
= User
::newFromName( $userText, false );
367 return $this->performer
;
370 public function getTarget() {
371 $namespace = $this->row
->rc_namespace
;
372 $page = $this->row
->rc_title
;
373 $title = Title
::makeTitle( $namespace, $page );
378 public function getTimestamp() {
379 return wfTimestamp( TS_MW
, $this->row
->rc_timestamp
);
382 public function getComment() {
383 return $this->row
->rc_comment
;
386 public function getDeleted() {
387 return $this->row
->rc_deleted
;
392 * Class for creating log entries manually, to inject them into the database.
396 class ManualLogEntry
extends LogEntryBase
{
397 /** @var string Type of log entry */
400 /** @var string Sub type of log entry */
403 /** @var array Parameters for log entry */
404 protected $parameters = [];
407 protected $relations = [];
409 /** @var User Performer of the action for the log entry */
410 protected $performer;
412 /** @var Title Target title for the log entry */
415 /** @var string Timestamp of creation of the log entry */
416 protected $timestamp;
418 /** @var string Comment for the log entry */
419 protected $comment = '';
421 /** @var int A rev id associated to the log entry */
422 protected $revId = 0;
424 /** @var array Change tags add to the log entry */
425 protected $tags = null;
427 /** @var int Deletion state of the log entry */
430 /** @var int ID of the log entry */
433 /** @var bool Can this log entry be patrolled? */
434 protected $isPatrollable = false;
436 /** @var bool Whether this is a legacy log entry */
437 protected $legacy = false;
441 * @param string $type
442 * @param string $subtype
444 public function __construct( $type, $subtype ) {
446 $this->subtype
= $subtype;
450 * Set extra log parameters.
452 * You can pass params to the log action message by prefixing the keys with
453 * a number and optional type, using colons to separate the fields. The
454 * numbering should start with number 4, the first three parameters are
455 * hardcoded for every message.
457 * If you want to store stuff that should not be available in messages, don't
458 * prefix the array key with a number and don't use the colons.
461 * $entry->setParameters(
462 * '4::color' => 'blue',
463 * '5:number:count' => 3000,
468 * @param array $parameters Associative array
470 public function setParameters( $parameters ) {
471 $this->parameters
= $parameters;
475 * Declare arbitrary tag/value relations to this log entry.
476 * These can be used to filter log entries later on.
478 * @param array $relations Map of (tag => (list of values|value))
481 public function setRelations( array $relations ) {
482 $this->relations
= $relations;
486 * Set the user that performed the action being logged.
489 * @param User $performer
491 public function setPerformer( User
$performer ) {
492 $this->performer
= $performer;
496 * Set the title of the object changed.
499 * @param Title $target
501 public function setTarget( Title
$target ) {
502 $this->target
= $target;
506 * Set the timestamp of when the logged action took place.
509 * @param string $timestamp
511 public function setTimestamp( $timestamp ) {
512 $this->timestamp
= $timestamp;
516 * Set a comment associated with the action being logged.
519 * @param string $comment
521 public function setComment( $comment ) {
522 $this->comment
= $comment;
526 * Set an associated revision id.
528 * For example, the ID of the revision that was inserted to mark a page move
529 * or protection, file upload, etc.
534 public function setAssociatedRevId( $revId ) {
535 $this->revId
= $revId;
539 * Set change tags for the log entry.
542 * @param string|string[] $tags
544 public function setTags( $tags ) {
545 if ( is_string( $tags ) ) {
552 * Set whether this log entry should be made patrollable
553 * This shouldn't depend on config, only on whether there is full support
554 * in the software for patrolling this log entry.
558 * @param bool $patrollable
560 public function setIsPatrollable( $patrollable ) {
561 $this->isPatrollable
= (bool)$patrollable;
565 * Set the 'legacy' flag
568 * @param bool $legacy
570 public function setLegacy( $legacy ) {
571 $this->legacy
= $legacy;
575 * Set the 'deleted' flag.
578 * @param int $deleted One of LogPage::DELETED_* bitfield constants
580 public function setDeleted( $deleted ) {
581 $this->deleted
= $deleted;
585 * Insert the entry into the `logging` table.
587 * @param IDatabase $dbw
588 * @return int ID of the log entry
589 * @throws MWException
591 public function insert( IDatabase
$dbw = null ) {
594 $dbw = $dbw ?
: wfGetDB( DB_MASTER
);
595 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
597 if ( $this->timestamp
=== null ) {
598 $this->timestamp
= wfTimestampNow();
601 // Trim spaces on user supplied text
602 $comment = trim( $this->getComment() );
604 // Truncate for whole multibyte characters.
605 $comment = $wgContLang->truncate( $comment, 255 );
607 $params = $this->getParameters();
608 $relations = $this->relations
;
610 // Additional fields for which there's no space in the database table schema
611 $revId = $this->getAssociatedRevId();
613 $params['associated_rev_id'] = $revId;
614 $relations['associated_rev_id'] = $revId;
619 'log_type' => $this->getType(),
620 'log_action' => $this->getSubtype(),
621 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
622 'log_user' => $this->getPerformer()->getId(),
623 'log_user_text' => $this->getPerformer()->getName(),
624 'log_namespace' => $this->getTarget()->getNamespace(),
625 'log_title' => $this->getTarget()->getDBkey(),
626 'log_page' => $this->getTarget()->getArticleID(),
627 'log_comment' => $comment,
628 'log_params' => LogEntryBase
::makeParamBlob( $params ),
630 if ( isset( $this->deleted
) ) {
631 $data['log_deleted'] = $this->deleted
;
634 $dbw->insert( 'logging', $data, __METHOD__
);
635 $this->id
= $dbw->insertId();
638 foreach ( $relations as $tag => $values ) {
639 if ( !strlen( $tag ) ) {
640 throw new MWException( "Got empty log search tag." );
643 if ( !is_array( $values ) ) {
644 $values = [ $values ];
647 foreach ( $values as $value ) {
650 'ls_value' => $value,
651 'ls_log_id' => $this->id
655 if ( count( $rows ) ) {
656 $dbw->insert( 'log_search', $rows, __METHOD__
, 'IGNORE' );
663 * Get a RecentChanges object for the log entry
666 * @return RecentChange
669 public function getRecentChange( $newId = 0 ) {
670 $formatter = LogFormatter
::newFromEntry( $this );
671 $context = RequestContext
::newExtraneousContext( $this->getTarget() );
672 $formatter->setContext( $context );
674 $logpage = SpecialPage
::getTitleFor( 'Log', $this->getType() );
675 $user = $this->getPerformer();
677 if ( $user->isAnon() ) {
678 // "MediaWiki default" and friends may have
679 // no IP address in their name
680 if ( IP
::isIPAddress( $user->getName() ) ) {
681 $ip = $user->getName();
685 return RecentChange
::newLogEntry(
686 $this->getTimestamp(),
689 $formatter->getPlainActionText(),
695 LogEntryBase
::makeParamBlob( $this->getParameters() ),
697 $formatter->getIRCActionComment(), // Used for IRC feeds
698 $this->getAssociatedRevId(), // Used for e.g. moves and uploads
699 $this->getIsPatrollable()
704 * Publish the log entry.
706 * @param int $newId Id of the log entry.
707 * @param string $to One of: rcandudp (default), rc, udp
709 public function publish( $newId, $to = 'rcandudp' ) {
710 DeferredUpdates
::addCallableUpdate(
711 function () use ( $newId, $to ) {
712 $log = new LogPage( $this->getType() );
713 if ( !$log->isRestricted() ) {
714 $rc = $this->getRecentChange( $newId );
716 if ( $to === 'rc' ||
$to === 'rcandudp' ) {
717 // save RC, passing tags so they are applied there
718 $tags = $this->getTags();
719 if ( is_null( $tags ) ) {
722 $rc->addTags( $tags );
723 $rc->save( 'pleasedontudp' );
726 if ( $to === 'udp' ||
$to === 'rcandudp' ) {
727 $rc->notifyRCFeeds();
730 // Log the autopatrol if the log entry is patrollable
731 if ( $this->getIsPatrollable() &&
732 $rc->getAttribute( 'rc_patrolled' ) === 1
734 PatrolLog
::record( $rc, true, $this->getPerformer() );
738 DeferredUpdates
::POSTSEND
,
743 public function getType() {
747 public function getSubtype() {
748 return $this->subtype
;
751 public function getParameters() {
752 return $this->parameters
;
758 public function getPerformer() {
759 return $this->performer
;
765 public function getTarget() {
766 return $this->target
;
769 public function getTimestamp() {
770 $ts = $this->timestamp
!== null ?
$this->timestamp
: wfTimestampNow();
772 return wfTimestamp( TS_MW
, $ts );
775 public function getComment() {
776 return $this->comment
;
783 public function getAssociatedRevId() {
791 public function getTags() {
796 * Whether this log entry is patrollable
801 public function getIsPatrollable() {
802 return $this->isPatrollable
;
809 public function isLegacy() {
810 return $this->legacy
;
813 public function getDeleted() {
814 return (int)$this->deleted
;